﻿Ext.define("PSI.App", {
    config: {
        userName: "",
        nimab:"",
        showRecent: false
    },
    
    constructor: function (config) {
        this.initConfig(config);
    },

    createMainUI: function () {
        this.mainPanel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "fit"
        });
       
        this.vp = Ext.create("Ext.container.Viewport", {
            layout: "fit",
            items: [{
                xtype: "panel",
                border: 0,
                layout: "border",
                bbar: ["当前用户：<span style='color:red'>" + this.getUserName() + "</span>"],
                items: [
                {
                    region: "center", layout: "fit", xtype: "container",
                    items: [this.mainPanel]
                }
                ]
            }]
        });

      //  Ext.getBody().mask("系统正在加载中...");

        Ext.Ajax.request({
            url: "/Home/GetMainMenuItems",
            method: "POST",
            callback: function (opt, success, response) {
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    this.createMainMenu(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误，请重新刷新浏览器");
                }
               // Ext.getBody().unmask();
            },
            scope: this
        });
    },

    getMainPanel: function () {
        return this.mainPanel;
    },

    // private
    createMainMenu: function (root) {
        var mp = this.vp.getComponent(0);
        var mainToolbar = Ext.create("Ext.toolbar.Toolbar", {
            dock: "top"
        });

        var menuItemClick = function () {
            if (this.fid == "-9999") {
                PSI.MsgBox.confirm("请确认是否重新登录", function () {
                    location.replace("/Home/NavigateTo?fid=-9999");
                });
            } else if (this.fid == "-9998") {
                if (Ext.isChrome) {
                    PSI.MsgBox.confirm("请确认是否退出系统?", function () {
                        window.open("", "_self", "");
                        window.close();
                    });
                } else if (Ext.isIE) {
                    window.close();
                }
            }
            else {
                location.replace("/Home/NavigateTo?fid=" + this.fid);
            }
        };

        var mainMenu = [];
        for (var i = 0; i < root.children.length; i++) {
             var m1 = root.children[i];

            var menuItem = Ext.create("Ext.menu.Menu");
            for (var j = 0; j < m1.children.length; j++) {
                var m2 = m1.children[j];

                if (m2.children.length == 0) {
                    // 只有二级菜单
                    menuItem.add({
                        text: m2.caption, fid: m2.fid, handler: menuItemClick
                    });
                } else {
                    var menuItem2 = Ext.create("Ext.menu.Menu");

                    menuItem.add({ text: m2.caption, menu: menuItem2 });

                    // 三级菜单
                    for (var k = 0; k < m2.children.length; k++) {
                        var m3 = m2.children[k];
                        menuItem2.add({
                            text: m3.caption, fid: m3.fid, handler: menuItemClick
                        });
                    }
                }
            }

            mainMenu.push({ text: m1.caption, menu: menuItem });
        }

        mainToolbar.add(mainMenu);

        mp.addDocked(mainToolbar);
    }
});